<template>
  <div class="app-container">
    <div>
      <el-form>
        <el-row class="rowFormItem">
          <el-col :span="3" style="margin-right: 10px;">
            <el-input v-model="keyword" placeholder="请输入预约名称进行搜索" />
          </el-col>
          <el-col :span="3">
            <el-select v-model="status" placeholder="预约状态">
              <el-option v-for="opt in statusList" :key="opt.id" :value="opt.id" :label="opt.label" />
            </el-select>
          </el-col>
          <!-- 提交按钮 -->
          <el-col :span="5" class="clear">
            <el-form-item style="text-align: center;width: 250px;margin-left: 10px;">
              <el-button type="primary" size="large" @click="handleSubmit()">检索</el-button>
              <el-button type="warning" size="large" style="margin-left: 8px" @click="handleReset()">重置</el-button>
<!--              <el-button type="success" size="large" @click="addmodel()">添加</el-button>-->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="tableClass">
      <el-table :data="formData" v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                border style="width: 100%;">
        <el-table-column align="center" prop="id" label="序号" />
        <el-table-column align="center" prop="realname" label="真实姓名" />
        <el-table-column align="center" prop="phone" label="手机号码" />
        <el-table-column align="center" prop="booking_time" label="预约日期" />
        <el-table-column align="center" prop="created_at" label="提交时间" />
        <el-table-column align="center" prop="" label="预约状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '0'" type="warning">预约中</el-tag>
            <el-tag v-if="scope.row.status === '1'" type="primary">确认预约</el-tag>
            <el-tag v-if="scope.row.status === '2'" type="success">完成预约</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="" label="操作">
          <template slot-scope="scope">
            <el-select v-model="scope.row.status" placeholder="预约状态" @change="statusChange($event, scope.row.id)">
              <el-option v-for="opt in statusList" :key="opt.id" :value="opt.id" :label="opt.label" />
            </el-select>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div style="text-align: center;">
        <el-pagination
          background layout="prev, pager, next" :current-page="page" @current-change="pageChange"
          :page-size="limit" :total="pageTotal" />
      </div>
    </div>

  </div>
</template>

<script>
  export default {
    name: "appointmentList",
    data() {
      return {
        limit: 10,
        page: 1,
        pageTotal: 0,
        keyword: "",
        status: "",
        loading: false,
        statusList: [
          {
            id: "0",
            label: "预约中",
          },
          {
            id: "1",
            label: "确认预约",
          },
          {
            id: "2",
            label: "完成预约",
          },
        ],
        formData: [],
      }
    },

    mounted() {
      this.getUserAppointmentList();
    },

    methods: {

      // 检索
      handleSubmit() {
        this.getUserAppointmentList()
      },

      // 重置
      handleReset() {
        this.keyword = "";
        this.status = "";
        this.getUserAppointmentList()
      },


      // 获取预约线下列表
      async getUserAppointmentList() {
        this.loading = true;
        const params = {
          keyword: this.keyword,
          status: this.status,
          limit: this.limit,
          page: this.page,
        };
        const res = await this.$api.getUserAppointmentList(params);
        if (res.data.level === "success") {
          this.formData = res.data.data;
          this.pageTotal = res.data.page_info.total;
        } else {
          this.$message.error(res.data.message);
        }
        this.loading = false;
      },

      // 分页
      pageChange(e) {
        this.page = e;
        this.getUserAppointmentList();
      },

      // 切换预约状态
      async statusChange(event, id) {
        const params = {
          id,
          status: event
        };
        const res = await this.$api.changeAppointmentStatus(params);
        this.$message({
          message: res.data.message,
          type: res.data.level,
        });
      },

    },

  }
</script>

<style lang="scss" scoped>

</style>
